import { Controller, Post, Body, Get, Param, UseGuards, Put } from '@nestjs/common';
import { ApiTags, ApiOperation } from '@nestjs/swagger';
import { UserService } from './user.service';
import { CreateUserDto, LoginDto } from './dto/user.dto';
import { JwtAuthGuard } from './jwt-auth.guard';

@ApiTags('用户')
@Controller('users')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Post('register')
  @ApiOperation({ summary: '用户注册' })
  async register(@Body() createUserDto: CreateUserDto) {
    return this.userService.createUser(createUserDto);
  }

  @Post('login')
  @ApiOperation({ summary: '用户登录' })
  async login(@Body() loginDto: LoginDto) {
    return this.userService.login(loginDto);
  }

  @Get(':id')
  @UseGuards(JwtAuthGuard)
  @ApiOperation({ summary: '获取用户信息' })
  async getUser(@Param('id') id: string) {
    return this.userService.getUserById(id);
  }

  @Put(':id/join-family')
  @UseGuards(JwtAuthGuard)
  @ApiOperation({ summary: '加入家庭' })
  async joinFamily(
    @Param('id') id: string,
    @Body('familyId') familyId: string,
  ) {
    return this.userService.updateUserFamily(id, familyId);
  }
}